﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;
using System.Text;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// An implementation of DataProxy that reads a data object from a object
    /// configured to reference a certain URL.
    /// 
    /// Note that this class cannot be used to retrieve data from a domain other
    /// than the domain from which the running page was served.
    ///
    /// For cross-domain access to remote data, use a ScriptTagProxy.
    /// 
    /// Be aware that to enable the browser to parse an XML document,
    /// the server must set the Content-Type header in the HTTP response to "text/xml".
    /// </summary>
    public class ExtHttpProxy : ExtProxy
    {
        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.data.HttpProxy", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.data.HttpProxy";

            String[] properties = new String[]{
                "autoAbort", "disableCaching", "timeout", "url", "method"       
            };

            Type component = typeof(ExtHttpProxy);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtHttpProxy.{0}'", property));

                Object value = i.GetValue(this, null);

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }

            return list;
        }

        #region Public Properties

        /// <summary>
        /// Whether a new request should abort any pending requests. (defaults to false)
        /// </summary>
        [Description("Whether a new request should abort any pending requests. (defaults to false)")]
        public Boolean? autoAbort { get; set; }

        /// <summary>
        /// True to add a unique cache-buster param to GET requests. (defaults to true)
        /// </summary>
        [DefaultValue(true)]
        [Description("True to add a unique cache-buster param to GET requests. (defaults to true)")]
        public Boolean? disableCaching { get; set; }

        /// <summary>
        /// The timeout in milliseconds to be used for requests. (defaults to 30000)
        /// </summary>
        [DefaultValue(30000)]
        [Description("The timeout in milliseconds to be used for requests. (defaults to 30000)")]
        public Int32? timeout { get; set; }

        /// <summary>
        /// The default URL to be used for requests to the server.
        /// </summary>        
        [Description("The default URL to be used for requests to the server.")]
        public String url { get; set; }

        /// <summary>
        /// The default HTTP method to be used for requests.
        /// (defaults to undefined; if not set but params are present will use "POST," otherwise "GET.")
        /// </summary>
        [Description("The default HTTP method to be used for requests.")]
        public httpMethod? method { get; set; }

        #endregion
    }
}
